#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "sikle.h"

void error_word_not_defined(Token* tok)
{
  int i; 
  char ch;

  NString name = tok->v.string;

  for(i = 0; ; i++) {
    if(tok->context[i] == '\n') {
      tok->context[i] = 0; 
      break;
    }
  }
  
  ch = name.data[name.size];
  printf("ERROR: @line %d: WORD(%s) not defined.\n\t->%s\n", tok->line, name.data, tok->context);
  name.data[name.size] = ch;
  exit(0);
}

void log_msg(LOG_LEVEL level, const char* msg, const Token* tok)
{
  if(tok != NULL) {
    int i; 

    for(i = 0; ; i++) {
      if(tok->context[i] == '\n') {
	tok->context[i] = 0; 
	break;
      }
    }

    switch(level) {
    case LOG_MESSAGE:
      printf("MESSAGE: %s\n", msg);
      break;
    case LOG_WARNING:
      printf("Warning: @line %d: %s\n\t->%s\n", tok->line, msg, tok->context);
      break;
    case LOG_ERROR:
      printf("ERROR: @line %d: %s\n\t->%s\n", tok->line, msg, tok->context);
      exit(0);
    case LOG_FATAL:
      printf("FATAL: @line %d: %s\n\t->%s\n", tok->line, msg, tok->context);
      exit(0);
    default:
      printf("ERROR: no such log level.\n");
      break;
    }
  }
  else {
    switch(level) {
    case LOG_MESSAGE:
      printf("MESSAGE: %s\n", msg);
      break;
    case LOG_WARNING:
      printf("WARNING: %s\n", msg);
      break;
    case LOG_ERROR:
      printf("ERROR: %s\n", msg);
      exit(0);
    case LOG_FATAL:
      printf("FATAL: %s\n", msg);
      exit(0);
    default:
      printf("ERROR: no such log level.\n");
      break;
    }
  }
}
